﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListViewSample
{
    public partial class MainForm : Form, IView
    {
        #region [ member fields ]

        private readonly Presenter m_presenter;

        #endregion

        public MainForm()
        {
            InitializeComponent();
            m_presenter = new Presenter(this);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            m_presenter.Start();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            m_presenter.Dispose();
        }

        #region [ implement IView ]
        public void AddNewMessage(string name, int id, float value)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action<string, int, float>(this.AddNewMessage), name, id, value);
            }
            else
            {
                string strid = id.ToString();
                string strvalue = value.ToString("0.###");

                var item = lvMessages.Items.Add(name);
                item.SubItems.Add(strid);
                item.SubItems.Add(strvalue);
                item.EnsureVisible();

                // ----------- fill texbox
                tbxName.Text = name;
                tbxId.Text = strid;
                tbxValue.Text = strvalue;
            }
        }

        public void Clear()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(this.Clear));
            }
            else
            {
                lvMessages.Items.Clear();
            }
        }
        #endregion
    }
}
